package com.itheima.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.result.JsonResult;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Flavor;
import com.itheima.entity.dto.DishDTO;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.FlavorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengbinghua
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/dish")
@Api(tags = "菜品分页")
public class DishController {
    @Autowired
    private  DishService dishService;
    @Autowired
    private FlavorService flavorService;
    @Autowired
    private CategoryService categoryService;
    /***菜品分页查询
     *
     * @return*/
    @GetMapping("page")
    @ApiOperation(value = "菜品分页查询")
    public JsonResult getDishinfoByPage(@ApiParam("名字") String categoryId,
                                        @ApiParam("类型") String status,
                                        @ApiParam("页码") Integer page,
                                        @ApiParam("每页显示条数") Integer pageSize) {
        //封装页面信息
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        //新建对象
        Page<DishDTO> dishDtoPage = new Page<>();
        //封装条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Dish::getCategoryId,categoryId);
        wrapper.eq(Dish::getStatus, "1");
        //分页查询
        dishService.page(pageInfo, wrapper);

        //拷贝属性，无视分页记录，分页记录另外处理
        BeanUtil.copyProperties(pageInfo, dishDtoPage, "records");
        //获得dish表的分页记录
        List<Dish> records = pageInfo.getRecords();
        //处理records集合，并且添加菜品名字，用于前端分页展示
        List<DishDTO> list = records.stream().map(dish -> {
            //新建dishdto对象
            DishDTO dishDTO = new DishDTO();
            //属性拷贝,这里dto少了菜品名这个属性
            BeanUtil.copyProperties(dish, dishDTO);
            //通过id获得菜品名字
            Category category = categoryService.getById(categoryId);
            if (null != category) {
                //获得菜品名
                String categoryName = category.getName();
                //dto设置属性
                dishDTO.setCategoryName(categoryName);
            }
            //通过dishid获得口味名字
            String dishId = dish.getId();
            LambdaQueryWrapper<Flavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Flavor::getDishId,dishId);
            //获得口味列表
            List<Flavor> flavorList = flavorService.list(wrapper1);
            dishDTO.setFlavorList(flavorList);
            //返回dto对象
            return dishDTO;
        }).collect(Collectors.toList());
        //给pagedto对象设置records属性值
        dishDtoPage.setRecords(list);
        //返回前端
        return JsonResult.success(dishDtoPage);
    }

}
